﻿using Prism.Ioc;
using Prism.Mvvm;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WpfApp_PrismViewModelLocator.ViewModels;
using WpfApp_PrismViewModelLocator.Views;

namespace WpfApp_PrismViewModelLocator
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            
        }

        /// <summary>
        /// ViewModel自定义匹配
        /// 
        /// 默认匹配规则1：
        /// View的类名称非View结尾的时候，解析ViewModel的类名称时候，后面需要加上ViewModel
        /// View的类名称以View结尾的时候，解析ViewModel的类名称时候，后面只需要加上Model
        /// </summary>
        protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator();

            //如果不是按照默认匹配规则创建的ViewModel也可以通过自行注册的方式
            //ViewModelLocationProvider.Register<MainWindow, MainWindowViewModel>();
            //通过字符串
            //ViewModelLocationProvider.Register("MainWindow", typeof(MainWindowViewModel));
            //通过委托
            //ViewModelLocationProvider.Register("MainWindow", () => 
            //{
            //    return Container.Resolve<MainWindowViewModel>();
            //});


            //修改配置默认的匹配规则
            //ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(_type => 
            //{
            //    var vmName = _type.FullName.Replace(".Views.", ".ViewModels.");

            //    if (vmName.EndsWith("View"))
            //        vmName += "Model";
            //    else
            //        vmName += "VM";

            //    return Type.GetType(vmName);
            //});
        }
    }
}
